package com.hupu.jrs.ui.imagepreview

import android.app.Fragment
import android.app.FragmentManager
import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.support.v13.app.FragmentStatePagerAdapter
import android.support.v4.view.ViewPager
import android.support.v7.widget.Toolbar
import android.view.Menu
import android.view.MenuItem
import android.view.ViewGroup
import java.util.ArrayList
import java.util.HashMap
import javax.inject.Inject
import butterknife.BindView
import butterknife.ButterKnife
import com.hupu.jrs.R
import com.hupu.jrs.ui.BaseSwipeBackActivity
import com.hupu.jrs.util.ResourceUtil.getStatusBarHeight
import com.hupu.jrs.util.StatusBarUtil

/**
 * Created by sll on 2016/3/10.
 */
class ImagePreviewActivity : BaseSwipeBackActivity(), ViewPager.OnPageChangeListener, ImagePreviewContract.View {

    @Inject
    internal var mPresenter: ImagePreviewPresenter? = null

    @BindView(R.id.viewPager)
    internal var viewPager: ViewPager? = null
    @BindView(R.id.toolbar)
    internal var toolbar: Toolbar? = null

    private val fragmentMap = HashMap<Int, ImageFragment>()
    private var mImageViewAdapter: ImageViewAdapter? = null
    private var mCurrentItem = 0
    private var extraPics: List<String>? = null
    private var extraPic: String? = null

    protected override val isApplyStatusBarTranslucency: Boolean
        get() = true

    protected override val isApplyStatusBarColor: Boolean
        get() = false

    override fun initContentView(): Int {
        return R.layout.activity_preview
    }

    override fun initInjector() {
        DaggerImagePreviewComponent.builder()
                .applicationComponent(getApplicationComponent())
                .activityModule(getActivityModule())
                .build()
                .inject(this)
    }

    override fun initUiAndListener() {
        ButterKnife.bind(this)
        mPresenter!!.attachView(this)
        toolbar!!.setPadding(0, getStatusBarHeight(this), 0, 0)
        toolbar!!.setBackgroundColor(Color.TRANSPARENT)
        initToolBar(toolbar)
        extraPics = getIntent().getStringArrayListExtra("extraPics")
        extraPic = getIntent().getStringExtra("extraPic")
        initViewPager()
        initCurrentItem()
    }

    private fun initViewPager() {
        mImageViewAdapter = ImageViewAdapter(getFragmentManager())
        viewPager!!.adapter = mImageViewAdapter
        viewPager!!.setOnPageChangeListener(this)
    }

    internal fun initCurrentItem() {
        mCurrentItem = extraPics!!.indexOf(extraPic)
        if (mCurrentItem < 0) {
            mCurrentItem = 0
        }
        viewPager!!.currentItem = mCurrentItem
        getSupportActionBar()!!.setTitle((mCurrentItem + 1).toString() + "/" + extraPics!!.size)
    }

    override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {

    }

    override fun onPageSelected(position: Int) {
        mCurrentItem = position
        getSupportActionBar()!!.setTitle((position + 1).toString() + "/" + mImageViewAdapter!!.count)
    }

    override fun onPageScrollStateChanged(state: Int) {

    }

    inner class ImageViewAdapter(fm: FragmentManager) : FragmentStatePagerAdapter(fm) {

        override fun getItem(position: Int): Fragment {
            var fragment: ImageFragment? = fragmentMap[position]
            if (fragment == null) {
                fragment = ImageFragment.newInstance(extraPics!![position])
                fragmentMap.put(position, fragment)
            }
            return fragment
        }

        override fun getCount(): Int {
            return extraPics!!.size
        }

        override fun setPrimaryItem(container: ViewGroup?, position: Int, `object`: Any) {
            super.setPrimaryItem(container, position, `object`)
            if (`object` is Fragment) {
                fragmentMap.put(position, `object` as ImageFragment)
            }
        }
    }

    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        getMenuInflater().inflate(R.menu.menu_picture, menu)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        val id = item.itemId
        if (id == R.id.savePicture) {
            mPresenter!!.saveImage(extraPics!![viewPager!!.currentItem])
        } else if (id == R.id.share) {

        } else if (id == R.id.copy) {
            mPresenter!!.copyImagePath(extraPics!![viewPager!!.currentItem])
        } else if (id == android.R.id.home) {
            finish()
        } else if (id == R.id.downloadAgain) {
            val fragment = mImageViewAdapter!!.getItem(viewPager!!.currentItem) as ImageFragment
            fragment?.initData()
        }
        return true
    }

    override fun setStatusBarColor(on: Boolean) {
        if (on) {
            StatusBarUtil.setColor(this, Color.TRANSPARENT, 0)
        }
    }

    companion object {

        fun startActivity(mContext: Context, extraPic: String, extraPics: ArrayList<String>) {
            val intent = Intent(mContext, ImagePreviewActivity::class.java)
            intent.putExtra("extraPic", extraPic)
            intent.putExtra("extraPics", extraPics)
            mContext.startActivity(intent)
        }
    }
}
